/**
 * 
 */
import java.util.*;
/**
 * @author 
 * Yannic Remmet			196983
 * Tobias Trabelsi			197112
 * Anna-Katharina Wickert	197122
 */
public class Library {
	
	public boolean sortedInsert(Book newBook){
		stock.add(newBook);
		return stock.contains(newBook);
	}
	public Book searchISBN(String isbn){
		Book i = stock.first();
		while (i != null){
			if (i.getISBN().equals(isbn))
				return i; 
			i = stock.higher(i);
		}
		return null;
	}
	public HashMap<String, String> stockByAuthor(String author){
		TreeSet<Book> authorSet = searchAuthor(author);
		HashMap<String, String> retMap = new HashMap<String, String>();
		Book i = authorSet.first();
		while (i!= null){
			retMap.put(i.getISBN(), i.getTitle());
			i = authorSet.higher(i);
		}
		
		return retMap;
	}
	
	public TreeSet<Book> searchAuthor(String author){
		TreeSet<Book> retSet = new TreeSet<Book>(new ISBNComparator());
		Book i = stock.first();
		while (i != null){
			if (i.getAuthor() == author)
				retSet.add(i);
			i = stock.higher(i);
		}
		return retSet;
		/*TreeSet<Book> retSet = new TreeSet<Book>(new AuthorComparator());
		retSet.addAll(stock);
		Book i = retSet.first();
		Book first = null;
		Book last = null; 
		if (searchAuthorT){
			for (Book b: retSet)
				System.out.println(" " + b);
			System.out.println(" --now the Stock-- ");
			for (Book b: stock)
				System.out.println(" " + b);
		}
		
		if (searchAuthorT)
			System.out.println(" Contains retSet all elements of Stock?: " + retSet.containsAll(stock) + " Contains Stock all elements of retSet " + stock.containsAll(retSet));
		assert retSet.contains(stock) : "Our stock sorted by the Author has different elements then our stock sorted by ISBN.";
		while (i != null){
			if (i.getAuthor().equals(author) && first == null) {
				first = i;
				if (searchAuthorT)
					System.out.println(" first " + first + " next " + retSet.higher(i));
				while (i != null && i.getAuthor().equals(author))
					i = retSet.higher(i);
				last = i;
				if (searchAuthorT)
					System.out.println(" first " + first + " last " + last);
			}
			if (searchAuthorT)
				System.out.println(" i " + i);
			i = retSet.higher(i);
		}
		return retSet.subSet(first, last); */
	}
	

	
	public TreeSet<Book> stock = new TreeSet<Book>(new ISBNComparator());
	
	//private boolean searchAuthorT = true;
}
